//
//  LGHeartRhythmHeadView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/30.
//

#import "LGHeartRhythmHeadView.h"

@interface LGHeartRhythmHeadView()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *baseView;

/**   */
@property (nonatomic , strong)UIView *cornerView;
/**  周 */
@property (nonatomic , strong)UILabel *weekLabel;
/**  月 */
@property (nonatomic , strong)UILabel *monthLabel;
/**  切换日期 */
@property (nonatomic , strong)UIButton *leftButton;
@property (nonatomic , strong)UIButton *rightBtn;
@property (nonatomic , strong)UILabel *dateLabel;
/**  总测量 */
@property (nonatomic , strong)UILabel *totalTitle;
@property (nonatomic , strong)UILabel *totalLabel;
/**  未见异常 */
@property (nonatomic , strong)UIButton *normalTitle;
@property (nonatomic , strong)UILabel *normalLabel;
/**  异常心律 */
@property (nonatomic , strong)UIButton *abnormalTitle;
@property (nonatomic , strong)UILabel *abnormalLabel;
/**  异常心律占比 */
@property (nonatomic , strong)UIButton *percentTitle;
@property (nonatomic , strong)UILabel *percentLabel;

/**   */
@property (nonatomic , strong)NSDate *currentDate;
/**   */
@property (nonatomic , strong)NSDateFormatter *monthFormatter;
/**   */
@property (nonatomic , strong)NSDateFormatter *weekFormatter;
/**   */
@property (nonatomic , strong)NSDateFormatter *dateFormatter;
/**   */
@property (nonatomic , strong)NSCalendar *calendar;
/**   */
@property (nonatomic , copy)NSString *startDate;
/**   */
@property (nonatomic , copy)NSString *endDate;

/**  week 、 month */
@property (nonatomic , copy)NSString *type;

@end

@implementation LGHeartRhythmHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.type = @"week";
        self.backgroundColor = [UIColor clearColor];
        self.calendar = [NSCalendar currentCalendar];
        self.currentDate = [NSDate date];
        [self createSubView];
        [self performSelector:@selector(requestData) withObject:nil afterDelay:0.2];
    }
    return self;
}

-(void)requestData{
    [self getWeekDate:self.currentDate];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.totalLabel.text = [NSString stringWithFormat:@"%@次",dataDic[@"allCount"]];
    self.normalLabel.text = [NSString stringWithFormat:@"%@次",dataDic[@"rightCount"]];
    self.abnormalLabel.text = [NSString stringWithFormat:@"%@次",dataDic[@"warnCount"]];
    self.percentLabel.text = [NSString stringWithFormat:@"%@%%",dataDic[@"warnProportion"]];
}

//切换周 、 月
-(void)switchDateTypeAction:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.baseView];
    if (point.x < (Screen_W-viewPix(32))/2.0 && [self.type isEqualToString:@"month"]) {
        //周
        self.type = @"week";
        self.currentDate = [NSDate date];
        [self getWeekDate:self.currentDate];
        [UIView animateWithDuration:0.3 animations:^{
            self.cornerView.frame = CGRectMake(viewPix(3), viewPix(3), (Screen_W-viewPix(38))/2.0, viewPix(30));
            self.weekLabel.textColor = [UIColor colorWithString:@"#282828"];
            self.weekLabel.font = LGFontWeight(14, UIFontWeightMedium);
            self.monthLabel.textColor = [UIColor colorWithString:@"#909399"];
            self.monthLabel.font = LGFont(14);
        }];
    }else if (point.x > (Screen_W-viewPix(32))/2.0 && [self.type isEqualToString:@"week"]){
        //月
        self.type = @"month";
        self.currentDate = [NSDate date];
        [self getMonthDate:self.currentDate];
        [UIView animateWithDuration:0.3 animations:^{
            self.cornerView.frame = CGRectMake((Screen_W-viewPix(38))/2.0, viewPix(3), (Screen_W-viewPix(38))/2.0, viewPix(30));
            self.monthLabel.textColor = [UIColor colorWithString:@"#282828"];
            self.monthLabel.font = LGFontWeight(14, UIFontWeightMedium);
            self.weekLabel.textColor = [UIColor colorWithString:@"#909399"];
            self.weekLabel.font = LGFont(14);
        }];
    }
}

//上、下个周或者月
-(void)switchDateAction:(UIButton *)sender{
    if ([self.type isEqualToString:@"week"]) {
        //周
        NSDateComponents *components = [self.calendar components:NSCalendarUnitWeekday fromDate:self.currentDate];
        components.weekday = (sender == self.leftButton)?-7:7;
        self.currentDate = [self.calendar dateByAddingComponents:components toDate:self.currentDate options:0];
        [self getWeekDate:self.currentDate];
    }else{
        //月
        NSDateComponents *components = [self.calendar components:NSCalendarUnitMonth fromDate:self.currentDate];
        components.month = (sender == self.leftButton)?-1:+1;
        self.currentDate = [self.calendar dateByAddingComponents:components toDate:self.currentDate options:0];
        [self getMonthDate:self.currentDate];
    }
    
}

-(void)getWeekDate:(NSDate *)currentDate{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:currentDate];
     
    // 获取今天是周几（周日是1，周一是2，以此类推）
    NSInteger weekday = components.weekday;
    
    // 获取这个星期的第一天（通常是星期日）
    NSDateComponents *startComp = [[NSDateComponents alloc] init];
    startComp.weekday = -weekday+2; // 因为weekday是从周日开始的，所以+1
   
    NSDateComponents *endComp = [[NSDateComponents alloc] init];
    endComp.weekday = 8-weekday; // 因为weekday是从周日开始的，所以+1
     
    NSDate *startDate = [calendar dateByAddingComponents:startComp toDate:currentDate options:0];
    NSDate *endDate = [calendar dateByAddingComponents:endComp toDate:currentDate options:0];
    
    self.startDate = [self.dateFormatter stringFromDate:startDate];
    self.endDate = [self.dateFormatter stringFromDate:endDate];
    
    self.dateLabel.text = [NSString stringWithFormat:@"%@-%@",[self.weekFormatter stringFromDate:startDate],[self.weekFormatter stringFromDate:endDate]];
    if (self.delegate && [self.delegate respondsToSelector:@selector(requestData:endDate:)]) {
        [self.delegate requestData:self.startDate endDate:self.endDate];
    }
}

-(void)getMonthDate:(NSDate *)currentDate{
    double interval = 0;
    NSDate *startDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&startDate interval:&interval forDate:currentDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [startDate dateByAddingTimeInterval:interval-1];
    }else {
        return;
    }

    self.startDate = [self.dateFormatter stringFromDate:startDate];
    self.endDate = [self.dateFormatter stringFromDate:endDate];
    self.dateLabel.text = [self.monthFormatter stringFromDate:self.currentDate];
    if (self.delegate && [self.delegate respondsToSelector:@selector(requestData:endDate:)]) {
        [self.delegate requestData:self.startDate endDate:self.endDate];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self addSubview:self.titleLabel];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cornerView];
    [self.baseView addSubview:self.weekLabel];
    [self.baseView addSubview:self.monthLabel];
    [self addSubview:self.leftButton];
    [self addSubview:self.dateLabel];
    [self addSubview:self.rightBtn];
    [self addSubview:self.totalLabel];
    [self addSubview:self.totalTitle];
    [self addSubview:self.normalLabel];
    [self addSubview:self.normalTitle];
    [self addSubview:self.abnormalLabel];
    [self addSubview:self.abnormalTitle];
    [self addSubview:self.percentLabel];
    [self addSubview:self.percentTitle];
    [self.totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(viewPix(24));
        make.bottom.equalTo(self.totalTitle.mas_top).offset(-viewPix(8));
    }];
    [self.totalTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalLabel);
        make.top.equalTo(self.baseView.mas_bottom).offset(viewPix(82));
    }];
    
    [self.normalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(-viewPix(40));
        make.centerY.equalTo(self.totalLabel);
    }];
    [self.normalTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.normalLabel);
        make.centerY.equalTo(self.totalTitle);
    }];
    
    [self.abnormalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_right).offset(-viewPix(105));
        make.centerY.equalTo(self.totalLabel);
    }];
    [self.abnormalTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.abnormalLabel);
        make.centerY.equalTo(self.totalTitle);
    }];
    
    [self.percentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalLabel);
        make.bottom.equalTo(self.percentTitle.mas_top).offset(-viewPix(8));
    }];
    [self.percentTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.percentLabel);
        make.top.equalTo(self.totalTitle.mas_bottom).offset(viewPix(47));
    }];
    
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"测量统计" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(18), viewPix(16), viewPix(150), viewPix(25));
    }
    return _titleLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(58), Screen_W-viewPix(32), viewPix(36))];
        _baseView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
        _baseView.cornerRidus = viewPix(18);
        [_baseView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(switchDateTypeAction:)]];
    }
    return _baseView;
}

-(UIView *)cornerView{
    if(!_cornerView){
        _cornerView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(3), viewPix(3), (Screen_W-viewPix(38))/2.0, viewPix(30))];
        _cornerView.backgroundColor = [UIColor whiteColor];
        _cornerView.cornerRidus = viewPix(15);
        
    }
    return _cornerView;
}

-(UILabel *)weekLabel{
    if(!_weekLabel){
        _weekLabel = [UILabel lableWithText:@"周" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _weekLabel.frame = CGRectMake(viewPix(3), 0, (Screen_W-viewPix(38))/2.0, viewPix(36));
        _weekLabel.userInteractionEnabled = NO;
    }
    return _weekLabel;
}

-(UILabel *)monthLabel{
    if(!_monthLabel){
        _monthLabel = [UILabel lableWithText:@"月" colorString:@"#909399" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _monthLabel.frame = CGRectMake(kMaxX(self.weekLabel.frame), 0, (Screen_W-viewPix(38))/2.0, viewPix(36));
        _monthLabel.userInteractionEnabled = NO;
    }
    return _monthLabel;
}

-(UIButton *)leftButton{
    if(!_leftButton){
        _leftButton = [UIButton buttonWithImage:@"日历-上个月" frame:CGRectMake(viewPix(90), kMaxY(self.baseView.frame)+viewPix(7), viewPix(30), viewPix(30))];
        [_leftButton addTarget:self action:@selector(switchDateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

-(UIButton *)rightBtn{
    if(!_rightBtn){
        _rightBtn = [UIButton buttonWithImage:@"日历-下个月" frame:CGRectMake(Screen_W-viewPix(120), kMaxY(self.baseView.frame)+viewPix(7), viewPix(30), viewPix(30))];
        [_rightBtn addTarget:self action:@selector(switchDateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _dateLabel.frame = CGRectMake(viewPix(110), kMaxY(self.baseView.frame)+viewPix(7), Screen_W-viewPix(220), viewPix(30));
    }
    return _dateLabel;
}

-(UILabel *)totalTitle{
    if(!_totalTitle){
        _totalTitle = [UILabel lableWithText:@"总测量" colorString:@"#909399" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _totalTitle;
}

-(UILabel *)totalLabel{
    if(!_totalLabel){
        _totalLabel = [UILabel lableWithText:@"--" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _totalLabel;
}

-(UIButton *)normalTitle{
    if(!_normalTitle){
        _normalTitle = [UIButton buttonWithTitle:@" 未见异常" titleFont:LGFont(14) textColor:@"#909399" imageName:@"心律监测-未见异常"];
        _normalTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _normalTitle.userInteractionEnabled = NO;
    }
    return _normalTitle;
}

-(UILabel *)normalLabel{
    if(!_normalLabel){
        _normalLabel = [UILabel lableWithText:@"--" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _normalLabel;
}

-(UIButton *)abnormalTitle{
    if(!_abnormalTitle){
        _abnormalTitle = [UIButton buttonWithTitle:@" 异常心律" titleFont:LGFont(14) textColor:@"#909399" imageName:@"心律监测-异常心律"];
        _abnormalTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _abnormalTitle.userInteractionEnabled = NO;
    }
    return _abnormalTitle;
}

-(UILabel *)abnormalLabel{
    if(!_abnormalLabel){
        _abnormalLabel = [UILabel lableWithText:@"--" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _abnormalLabel;
}

-(UIButton *)percentTitle{
    if(!_percentTitle){
        _percentTitle = [UIButton buttonWithTitle:@" 异常心律占比" titleFont:LGFont(14) textColor:@"#909399" imageName:@"心律监测-异常心律占比"];
        _percentTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _percentTitle.userInteractionEnabled = NO;
    }
    return _percentTitle;
}

-(UILabel *)percentLabel{
    if(!_percentLabel){
        _percentLabel = [UILabel lableWithText:@"--" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _percentLabel;
}


-(NSDateFormatter *)dateFormatter{
    if(!_dateFormatter){
        _dateFormatter = [[NSDateFormatter alloc]init];
        _dateFormatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dateFormatter;
}

-(NSDateFormatter *)weekFormatter{
    if(!_weekFormatter){
        _weekFormatter = [[NSDateFormatter alloc]init];
        _weekFormatter.dateFormat = @"MM月dd日";
    }
    return _weekFormatter;
}

-(NSDateFormatter *)monthFormatter{
    if(!_monthFormatter){
        _monthFormatter = [[NSDateFormatter alloc]init];
        _monthFormatter.dateFormat = @"yyyy年MM月";
    }
    return _monthFormatter;
}
@end
